package com.bookstore.admin.orders.handler;

import com.bookstore.admin.orders.service.IOdersServiceImpl;
import com.bookstore.admin.orders.service.IOrdersService;
import com.bookstore.client.order.service.IOrderService;
import com.bookstore.commons.beans.Order;
import com.bookstore.commons.beans.OrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/admin/orders")
public class OrdersHandler {

    @Autowired
    IOrdersService ordersService;

   /*查询所有订单列表*/
    @RequestMapping("/findOrders")
    public String findOrders(Model model){
        List<Order> orders = ordersService.findAllorders();
        /* for (Order o:orders){
             System.out.println(o);
         }*/
         model.addAttribute("orders",orders);
         return "/admin/orders/list.jsp";
    }

    /*多条件查询*/
   @RequestMapping("/findOrderByManyCondition")
    public String findOrderByManyCondition(Order order,Model model){
       List<Order> orders = ordersService.findOrderByManyCondition(order);
        /*for (Order o:orders){
           System.out.println(o);
        }*/
        model.addAttribute("orders",orders);
        model.addAttribute("order",order);
        return "/admin/orders/list.jsp";
   }

   /*查看订单项*/
  @RequestMapping("/findOrderById")
   public String findOrderById(Order order, Model model){
      order = ordersService.selectOrderById(order);
      List<OrderItem> orderItems = ordersService.findOrderItemById(order.getId());
      System.out.println(order);
      for (OrderItem or:orderItems){

          System.out.println(orderItems);
      }
      model.addAttribute("order",order);
      model.addAttribute("orderItems",orderItems);
      return "/admin/orders/view.jsp";
  }

  @RequestMapping("/delOrderById")
    public String delOrderById(String id){
      ordersService.removeOrders(id);
      return "redirect:/admin/orders/findOrders";
  }


}
